package com.example.demo.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.mapper.ClickHouse.ClickHouseMapper;
import com.example.demo.mapper.OrderMapper;
import com.example.demo.mapper.PutOrderMapper;
import com.example.demo.pojo.Order.Orders;
import com.example.demo.pojo.Stock.Stock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
@Slf4j
public class StockUtilsNew {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private PutOrderMapper putOrderMapper;
    @Resource
    private ClickHouseMapper clickHouseMapper;

    //判断股票是哪个板块的,如北交所or深交or沪交or创业板or新三板
    public String judgeSector(String stockCode) {
        // 创业板
        String regex_chuanYeBan = "^300\\d{3}$";
        Pattern pattern_chuanYeBan = Pattern.compile(regex_chuanYeBan);
        // 科创板
        String regex_keChuanBan = "^688\\d{3}$";
        Pattern pattern_keChuanBan = Pattern.compile(regex_keChuanBan);
        // 北交所
        String regex_beiJiaoSuo1 = "^8\\d{5}$";
        Pattern pattern_beiJiaoSuo1 = Pattern.compile(regex_beiJiaoSuo1);
        String regex_beiJiaoSuo2 = "^43\\d{4}$";
        Pattern pattern_beiJiaoSuo2 = Pattern.compile(regex_beiJiaoSuo2);
        // 沪市主板
        String regex_shangHaiZhuBan = "^60\\d{4}$";
        Pattern pattern_shangHaiZhuBan = Pattern.compile(regex_shangHaiZhuBan);
        // 深市主板
        String regex_shenZhenZhuBan = "^00\\d{4}$";
        Pattern pattern_shenZhenZhuBan = Pattern.compile(regex_shenZhenZhuBan);

        // 匹配
        Matcher matcher = null;
        if (pattern_chuanYeBan.matcher(stockCode).matches()) {
            return "创业板";
        }
        if (pattern_keChuanBan.matcher(stockCode).matches()) {
            return "科创板";
        }
        if (pattern_beiJiaoSuo1.matcher(stockCode).matches()
                || pattern_beiJiaoSuo2.matcher(stockCode).matches()
        ) {
            return "北交所";
        }
        if (pattern_shangHaiZhuBan.matcher(stockCode).matches()) {
            return "沪市主板";
        }
        if (pattern_shenZhenZhuBan.matcher(stockCode).matches()) {
            return "深市主板";
        }
        return null;
    }

    // 今天是否为交易日？
    public Boolean isTradingDay(String date){
        // 查询上证指数今天数值是否有变动，有的话说明为交易日
        return true;
    }

    //判断是否可以交易此股票，今天是否购买过此股票
    public boolean canTradeStock(Integer id, Integer userId, String stockCode) {
        // 在Clickhouse数据库中查询是否能交易
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.now();
        Stock stock = clickHouseMapper.getStockInfo(stockCode,localDateTime);
        //
        // 查询订单信息
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", id)  // 确保字段名与数据库中的一致
                .eq("user_id", userId)  // 确保字段名与数据库中的一致
                .eq("stock_code", stockCode)  // 确保字段名与数据库中的一致
                .eq("order_type", "买入")  // 确保字段名与数据库中的一致
                // 如果time字段包含时间，确保只比较日期部分
                .apply("DATE_FORMAT(time, '%Y-%m-%d') = {0}", LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        List<Orders> ordersList = orderMapper.selectList(queryWrapper);
        log.info(ordersList.toString());
        // 查询挂单信息
//        QueryWrapper<PutOrders> putOrdersQueryWrapper = new QueryWrapper<>();
//        putOrdersQueryWrapper.eq("order_id", id)
//                .eq("user_id", userId)
//                .eq("stock_code", stockCode)
//                .eq("order_type", "买入")
//                // 根据今天的日期添加查询条件
//                .apply("DATE_FORMAT(time, '%Y-%m-%d') = {0}", LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
//        List<PutOrders> putOrders = putOrderMapper.selectList(putOrdersQueryWrapper);
        // 判断是否可以交易
        return (ordersList.size() > 0);
    }


}
